package com.tourism.tourismGYM.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.tourism.tourismGYM.commom.BasePage;
import com.tourism.tourismGYM.dao.TourismApmDao;
import com.tourism.tourismGYM.entity.TourismApm;
import com.tourism.tourismGYM.entity.TourismRm;
import com.tourism.tourismGYM.service.TourismApmService;
import com.tourism.tourismGYM.service.TourismRmService;

/**
 * 活动项目服务类
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class TourismApmServiceImpl implements TourismApmService {
	@Autowired
	private TourismApmDao tourismApmDao;
	@Autowired
	private TourismRmService tourismRmService;
	@Override
	public Page<HashMap> page(Map<String, Object> condition) {
		BasePage basePage = new BasePage(condition);
		Pageable pageable = basePage.getPageable("");
		StringBuilder dataSql = new StringBuilder("select * from tourism_apm t where 1=1 ");
		if (condition.get("type")!=null&&!"".equals(condition.get("type").toString())) {
			dataSql.append(" and t.active_type = " + "'" + condition.get("type").toString() + "'");
		}
		if (condition.get("param")!=null&&!"".equals(condition.get("param").toString())) {
			dataSql.append(" and t.active_name like " + "'%" + condition.get("param") + "%'");
		}
		dataSql.append(" order by t.create_time desc");
		Page<HashMap> page = tourismApmDao.page(pageable, dataSql);
		return page;
	}

	@Override
	public TourismApm create(TourismApm tourismApm) {
		tourismApm.setCreateTime(new Date());
		tourismApm.setIsStart("0");
		return tourismApmDao.save(tourismApm);
	}

	@Override
	public List<TourismApm> getAll() {
		// TODO Auto-generated method stub
		return tourismApmDao.findAll();
	}

	@Override
	public TourismApm getById(String id) {
		// TODO Auto-generated method stub
		return tourismApmDao.findOne(id);
	}

	@Override
	public TourismApm update(TourismApm tourismApm) {
		return tourismApmDao.save(tourismApm);
	}

	@Override
	public boolean delete(String id) {
		boolean flag = true;
		List<TourismRm> tourismRms = tourismRmService.getByApmId(id);
		if(tourismRms.size()>0) {
			flag = false;
		}else {
			tourismApmDao.delete(id);
		}
		return flag;
	}
}
